﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class admin_Pages : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            LoadData();
            LoadPage();
            btn_save.Attributes.Add("style", "float:none");
        }
        Utilities.SetLabel(Master, "Pages");
    }

    private void LoadData()
    {
        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT * FROM dbo.Fn_GetAllLanguages(0, 1000000, 'id', 'asc')", conn))
            {
                SqlDataReader dr = null;

                dr = cmd.ExecuteReader();
                if (dr != null)
                {
                    if (dr.HasRows)
                    {
                        while (dr.Read())
                        {
                            ddl_language.Items.Add(new ListItem(dr["language_name"].ToString(), dr["id"].ToString()));
                        }
                    }
                }
                dr.Close();
            }
        }

        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT * FROM dbo.Fn_GetAllPageType()", conn))
            {
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr != null)
                {
                    if (dr.HasRows)
                    {
                        while (dr.Read())
                        {
                            ddl_page.Items.Add(new ListItem(dr["desc"].ToString(), dr["id"].ToString()));
                        }
                    }
                }
                dr.Close();
            }
        }
    }
    protected void ddl_language_SelectedIndexChanged(object sender, EventArgs e)
    {
        LoadPage();
    }

    private void LoadPage()
    {
        string lang = ddl_language.SelectedValue;
        string page = ddl_page.SelectedValue;
        if (lang != "-1" && page != "-1")
        {
            using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("SELECT * from dbo.Fn_GetPage(@lang_id,@page_id)", conn))
                {
                    SqlDataReader dr = null;
                    SqlParameter[] pars = new SqlParameter[2];
                    pars[0] = new SqlParameter("lang_id", lang);
                    pars[1] = new SqlParameter("page_id", page);

                    cmd.Parameters.Add(pars[0]);
                    cmd.Parameters.Add(pars[1]);

                    dr = cmd.ExecuteReader();
                    if (dr != null)
                    {
                        if (dr.HasRows)
                        {
                            if (dr.Read())
                            {
                                uc_html_editor1.htmlCode = dr["html"].ToString();
                                tbKeywords.Text = dr["keywords"].ToString();
                                hf_id.Value = dr["id"].ToString();
                            }
                        }
                        else
                        {
                            uc_html_editor1.htmlCode = "";
                            tbKeywords.Text = "";
                            hf_id.Value = "";
                        }
                    }
                    dr.Close();
                }
            }
        }
    }
    protected void ddl_page_SelectedIndexChanged(object sender, EventArgs e)
    {
        LoadPage();
    }
    protected void btn_save_Click(object sender, EventArgs e)
    {
        if (hf_id.Value == "")
        {
            //insert
            using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("SP_InsPage", conn))
                {
                    SqlParameter[] pars = new SqlParameter[4];
                    pars[0] = new SqlParameter("page_id", ddl_page.SelectedValue);
                    pars[1] = new SqlParameter("html", uc_html_editor1.htmlCode);
                    pars[2] = new SqlParameter("keywords", tbKeywords.Text);
                    pars[3] = new SqlParameter("language_id", ddl_language.SelectedValue);
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    foreach (SqlParameter s in pars)
                        cmd.Parameters.Add(s);
                    try
                    {
                        object val = cmd.ExecuteScalar();
                        Session["message"] = " Insert was succesfull<br />";
                        Session["message_type_css"] = "par_message_ok";
                    }
                    catch (Exception ex)
                    {
                        Session["message"] += " There was an error inserting a new page " + ex.ToString() + "<br />";
                        Session["message_type_css"] = "par_message_error";
                    }
                    cmd.Parameters.Clear();
                }
            }
        }
        else
        {
            //update
            using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("SP_UpdatePage", conn))
                {
                    SqlParameter[] pars = new SqlParameter[3];
                    pars[0] = new SqlParameter("html", uc_html_editor1.htmlCode);
                    pars[1] = new SqlParameter("keywords", tbKeywords.Text);
                    pars[2] = new SqlParameter("id", hf_id.Value);
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    foreach (SqlParameter s in pars)
                        cmd.Parameters.Add(s);
                    try
                    {
                        int val = cmd.ExecuteNonQuery();
                        Session["message"] = " Insert was succesfull<br />";
                        Session["message_type_css"] = "par_message_ok";
                    }
                    catch (Exception ex)
                    {
                        Session["message"] += " There was an error inserting a new page " + ex.ToString() + "<br />";
                        Session["message_type_css"] = "par_message_error";
                    }
                    cmd.Parameters.Clear();
                }
            }
        }

        Response.Redirect(Request.Url.ToString());
    }
}
